package com.example.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Fanout：FanoutExchange 的数据交换策略是把所有到达 FanoutExchange 的消息转发给所有与它绑定的 Queue 上，
 * 在这种策略中，routingkey 将不起任何作用，FanoutExchange
 */
@Configuration
public class FanoutConfig {

    public static final String FANOUT_QUEUE_NAME = "fanout_queue_name";
    public static final String FANOUT_QUEUE_NAME2 = "fanout_queue_name2";
    public static final String FANOUT_EXCHANGE_NAME = "fanout_exchange_name";

    @Bean
    Queue fnaoutQueue1() {
        return new Queue(FANOUT_QUEUE_NAME, true, false, false);
    }

    @Bean
    Queue fnaoutQueue2() {
        return new Queue(FANOUT_QUEUE_NAME2, true, false, false);
    }

    /**
     * 定义一个直连交换机
     *
     * @return
     */
    @Bean
    FanoutExchange fanoutExchange() {
        //1. 交换机的名称
        //2。交换机是否持久化
        //3. 如果没有与之绑定的队列，是否删除交换机
        return new FanoutExchange(FANOUT_EXCHANGE_NAME, true, false);
    }

    /**
     * 这个定义是将交换机和队列绑定起来
     *
     * @return
     */
    @Bean
    Binding fanoutBinding1() {
        return BindingBuilder
            //设置绑定的队列
            .bind(fnaoutQueue1()).to(fanoutExchange());
    }

    @Bean
    Binding fanoutBinding2() {
        return BindingBuilder
            //设置绑定的队列
            .bind(fnaoutQueue2()).to(fanoutExchange());
    }
}
